package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.magic.gre.R;
import com.magic.gre.adapter.PagerAdapterHome;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.entity.SerializableMap;
import com.magic.gre.mvp.contract.GuideContract;
import com.magic.gre.mvp.presenter.GuidePresenterImpl;
import com.magic.gre.ui.fragment.Guide1Fragment;
import com.magic.gre.ui.fragment.Guide2Fragment;
import com.magic.gre.ui.fragment.Guide3Fragment;
import com.magic.gre.ui.fragment.Guide4Fragment;
import com.magic.gre.ui.fragment.Guide5Fragment;
import com.magic.gre.ui.fragment.Guide6Fragment;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.widget.UnScrollViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * Author: Lmoumou
 * Date: 2019-01-11 16:25
 * 文件名: GuideActivity
 * 描述:
 */
public class GuideActivity extends BaseMVPActivity<GuidePresenterImpl> implements GuideContract.View {

    @BindView(R.id.mViewPager)
    UnScrollViewPager mViewPager;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), GuideActivity.class);
        context.startActivity(intent);
    }

    private ArrayList<Fragment> fragmentList = new ArrayList<>();
    private Map<String, Object> parmsMap;
    private SerializableMap serializableMap;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    protected boolean isImmerse() {
        return true;
    }

    @Override
    protected GuidePresenterImpl creatPresenter() {
        return new GuidePresenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void handlerMsg(MsgEvent msgEvent) {
        super.handlerMsg(msgEvent);
        switch (msgEvent.getEvent()) {
            case MsgEvent.GUIDE_NEXT:
                int nextPosition = (int) msgEvent.get("next");
                mViewPager.setCurrentItem(nextPosition);
                break;
            case MsgEvent.GUIDE_SUMBIT:
                mPresenter.pSaveGuide(serializableMap.getMap());
                break;
        }
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        registerRxBus();
        serializableMap = new SerializableMap();
        parmsMap = new HashMap<>();
        serializableMap.setMap(parmsMap);
        fragmentList.clear();
        fragmentList.add(Guide1Fragment.newInstance());
        fragmentList.add(Guide2Fragment.newInstance(serializableMap));
        fragmentList.add(Guide3Fragment.newInstance(serializableMap));
        fragmentList.add(Guide4Fragment.newInstance(serializableMap));
        fragmentList.add(Guide5Fragment.newInstance(serializableMap));
        fragmentList.add(Guide6Fragment.newInstance(serializableMap));
        mViewPager.setAdapter(new PagerAdapterHome(getSupportFragmentManager(), fragmentList));
    }

    @Override
    public void vSaveGuide() {
        MainActivity.startThis(this);
        finish();
    }

    @Override
    public void onBackPressed() {
        int currentItem = mViewPager.getCurrentItem();
        if (currentItem > 0) {
            mViewPager.setCurrentItem(currentItem - 1);
        } else {
            super.onBackPressed();
        }

    }
}
